package com.lolineet.mqtt.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lolineet.mqtt.data.PointValue;
import org.springframework.integration.mqtt.support.MqttHeaders;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHandler;
import org.springframework.messaging.MessagingException;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;



/**
 * @Auther lolineet
 * @Email tj.zhanghongwei@163.com
 * @Date 2021/11/23 14:12
 * @Description mqtt消息接收器
 */
@Component
public class MqttMessageReceiver implements MessageHandler {


    @Override
    public void handleMessage(Message<?> message) throws MessagingException {
        try {
            String topic = String.valueOf(message.getHeaders().get(MqttHeaders.RECEIVED_TOPIC));
            String payload = (String) message.getPayload();
            System.out.println("获取到的消息的payload :" + payload);
            JSONObject data= JSON.parseObject(payload);
            PointValue.pointValue.put(topic,data);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }
}
